package com.example.passport.controller;

import com.example.common.dto.JsonResult;
import com.example.interceptor.UserContext;
import com.example.passport.dto.TokenCreateDto;
import com.example.passport.dto.TokenDto;
import com.example.passport.entity.User;
import com.example.passport.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

@Api(tags = "身份验证")
@RestController
@CrossOrigin
@RequestMapping("/passport/token")
public class TokenController {

    @Resource
    public TokenService tokenService;

    @ApiOperation("登录")
    @PostMapping("/create")
    public JsonResult<TokenDto> tokenCreate(@Valid TokenCreateDto tokenCreateDto) {
        return new JsonResult<>(tokenService.createToken(tokenCreateDto.getMobile(), tokenCreateDto.getPassword(),tokenCreateDto.getIp(),tokenCreateDto.getLoginEquipment()));
    }

    @ApiOperation("退出登录")
    @PostMapping("/delete")
    public JsonResult<String> delete(@Valid TokenDto tokenDto) {
        User user = UserContext.getUser();

        tokenService.deleteToken(tokenDto.getToken(), user.getId());

        return new JsonResult<>(true, "退出成功");
    }

}
